40923225 cp2020

  • Home
    • Site Map
    • reveal
    • blog
  • Homework1(作業1)
    • PCH10.Networking Hardware(網路硬件)
      • Network Devices(網路設備)
      • Install a Network Adaper(安裝網路適配器)
    • PCH13.IPv4 and IPv6 Addresses(IPv4和IPv6的地址)
      • IPv4 and IPv6 Addresses(IPv4和IPv6的地址)
      • IPv4 Addressing(IPv4編址)
      • IPv4 Address Classes(IPv4網址類別)
      • Assigning an IP Address(分配IP網址)
      • IPv6
      • Classful Vs. Classless Addressing.Subnet and CIDR(相對於無類尋址.子網和CIDR)
      • Windows Networking(Windows網絡)
  • W8 cp2020
  • Homework2(作業2)
    • 2-1(分組)
  • Homework3(作業3)
    • Check Primality Functions (檢查功能)11
    • Divisors(除數)4
    • File Overlap Solutions(文件重疊)23
Divisors(除數)4 << Previous

File Overlap Solutions(文件重疊)23

Exercise 23(練習23)

Given two .txt files that have lists of numbers in them, find the numbers that are overlapping. One .txt file has a list of all prime numbers under 1000, and the other .txt file has a list of happy numbers up to 1000.

(給定.txt其中有數字列表的兩個文件,請查找重疊的數字。一個.txt文件包含所有小於1000的質數的列表,另一個.txt文件包含不超過1000的快樂數的列表。)

solution(解答)

def file_to_list(file_to_convert):
with open(file_to_convert, "r") as open_file:
list_to_return = []
line = open_file.readline()
while line:
list_to_return.append(int(line))
line = open_file.readline()
return list_to_return

def overlapping_numbers(file_one, file_two):
return set(file_to_list(file_one)) & set(file_to_list(file_two))

if __name__ == "__main__":
print(sorted(overlapping_numbers("primenumbers.txt", "happynumbers.txt")))


Divisors(除數)4 << Previous

Copyright © All rights reserved | This template is made with by Colorlib